package cn.com.note.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.support.v7.widget.AppCompatTextView;
import android.util.Log;

import cn.com.note.R;

/**
 * Created by SunFuLin on 2019/3/1.
 */

public class HomeItemView extends AppCompatTextView{
    Paint mPaint ;
    private Context mContext;
    private int colorD,colorL;

    private int mWidth;
    private int mHeight;
    //圆角大小
    private float mRadius,rightSpot;


    public HomeItemView(Context context) {
        super(context);
        initView(context, null);
    }

    public HomeItemView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        this.mContext=context;
        TypedArray array=context.obtainStyledAttributes(attrs, R.styleable.HomeItemView);
        mRadius=array.getDimension(R.styleable.HomeItemView_radius,0);
        rightSpot=array.getFloat(R.styleable.HomeItemView_right_spot,0);
        colorD=array.getColor(R.styleable.HomeItemView_color_Dark,ContextCompat.getColor(mContext, R.color.colorPrimary));
        colorL=array.getColor(R.styleable.HomeItemView_color_Light,ContextCompat.getColor(mContext, R.color.colorAccent));
        array.recycle();
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.FILL);
    }

    public HomeItemView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();
        Log.d("gaodui",""+mHeight);
    }
    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        canvas.drawBitmap(createRoundConerImage(createSourceImage()), 0, 0, null);
    }
    /**
     * 绘制圆角
     * @return
     */
    private Bitmap createSourceImage() {
        final Paint paint = new Paint();
        paint.setAntiAlias(true);
        Bitmap target = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(target);
        paint.setColor(colorD);
        drawCircle(canvas,paint);
        drawRect(canvas,paint);
        canvas.drawBitmap(target, 0, 0, paint);
        return target;
    }
    /**
     * 绘制圆角
     * @param source
     * @return
     */
    private Bitmap createRoundConerImage(Bitmap source) {
        final Paint paint = new Paint();
        paint.setAntiAlias(true);
        Bitmap target = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(target);
        RectF rect = new RectF(0, 0, getMeasuredWidth(), getMeasuredHeight());
        canvas.drawRoundRect(rect, mRadius, mRadius, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(source, 0, 0, paint);
        return target;
    }

    /**
     * 右边
     * @param canvas
     * @param paint
     */
    private void drawCircle(Canvas canvas, Paint paint){
        float factor=mWidth*0.1f;
        float bW=mWidth*0.32f;
        float bigCx=mWidth+factor;
        float bigR=(float) Math.sqrt(mHeight*mHeight+(bW+factor)*(bW+factor));
        float smaCx=mWidth+factor;
        float smaCy=mHeight-factor;
        float smaR=(float) Math.sqrt((bW+factor)*(bW+factor)+factor*factor);
        canvas.drawCircle(bigCx, 0, bigR, paint);
        paint.setColor(colorL);
        canvas.drawCircle(smaCx, smaCy, smaR, paint);

    }

    /**
     * 绘制左边
     */
    private void drawRect(Canvas canvas, Paint paint){
        float factor=mWidth*0.06f;
        RectF rectF=new RectF(-factor,mHeight-factor,factor,mHeight+factor);
        Path mPath=new Path();
        mPath.moveTo(0, mHeight);
        mPath.lineTo(0, mHeight-factor);
        mPath.lineTo(factor, mHeight);
        mPath.close();
        canvas.drawPath(mPath, paint);
    }
}
